package com.spark.common.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.spark.common.R
import com.spark.common.model.BorrowListModel

class DataView(context: Context, attrs: AttributeSet?, defStyle: Int) : LinearLayout(context, attrs, defStyle) {
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null, 0)

    init {
        orientation = VERTICAL
    }

    fun bind(books: MutableList<BorrowListModel.BorrowItemModel.BookModel>) {
        books.forEach {
            val child = DataItemView(context)
            child.bind(it.name)
            addView(child)
        }
    }
}

class DataItemView(context: Context, attrs: AttributeSet?, defStyle: Int) : LinearLayout(context, attrs, defStyle) {
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null, 0)

    private val tvName: TextView = rootView.findViewById(R.id.tv_name)

    override fun getRootView(): View {
        return LayoutInflater.from(context).inflate(R.layout.item_borrow_material, this)
    }

    fun bind(name: String) {
        tvName.text = name
    }

}